// pages/mycenter/mycenter.js
import { Mycenter } from '../../model/mycenter.js'
const mycenter = new Mycenter();
Page({

    /**
     * 页面的初始数据
     */
    data: {
      my_details:{},
      piclist:[],
      id:'',
      fileName:'',
      page:0,
      myhead_img:'',
      userobj:{},
      isShowCommodity:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      var that=this;
      wx.getStorage({
        key: 'userinfo',
        success (res) {
          console.log(res.data)
          that.setData({
            userobj:res.data,
          })
          

          console.log(that.data.userobj)
        }
      })
    },
    //用户信息
    my_detail(){
      mycenter.my_center().then(res=>{
          
          this.setData({
              my_details:res.data.user,
              isShowCommodity:res.data.isShowCommodity
              
          })
          console.log(res)
          console.log(res.data.user.nickName)
          console.log(res.data.user.jobNo)
          console.log(res.data.user.email)
          console.log(res.data.user.headPortrait)
      }).catch(error=>{
          console.log(error)
      })
  },
    activity:function(){
        wx.navigateTo({
          url: '/pages/myactivity/myactivity',
        })
    },
    exchange:function(){
        wx.navigateTo({
          url: '/pages/integralshop/integralshop',
        })
    },
    feedback:function(){
        wx.navigateTo({
          url: '/pages/opinion/opinion',
        })
    },
    // about_us:function(){
    //     wx.navigateTo({
    //       url: '/pages/our/our',
    //     })
    // },
    install:function(){
        wx.navigateTo({
          url: '/pages/install/install',
        })
    },
    tohtml(e){
      console.log(e)
        // var id = e.currentTarget.dataset.item.id;
        // var type = e.currentTarget.dataset.item.type;
        
          wx.navigateTo({
            url: '/pages/html5/html5?type=2&id=',
          })
        
    },
    //更换头像
    // 上传
    upimages(){
      var that = this
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          console.log(res)
          const tempFilePaths = res.tempFilePaths
          
          var userobj=that.data.userobj
          userobj.headPortrait=res.tempFilePaths
          that.setData({
            userobj:userobj,
            hidden:true
          })
          // that.uploadsome(tempFilePaths)


              
              console.log(tempFilePaths)
              // return false
              wx.showLoading({
                title: '上传中',
              })

              wx.uploadFile({
                url: 'https://api.changetraining.com.cn/qiniu/upload_file',
                filePath: tempFilePaths[0],
                name: 'file',
                formData: {
                  'user': 'test',
                },
                success(res) {
                  console.log(res.data);
                  console.log(res)
                  var msg = JSON.parse(res.data)
                  var my_details = that.data.my_details
                  my_details.headPortrait = msg.data.httpPicUrl
                  that.setData({
                    my_details:my_details
                  })  
                  that.portrait(msg.data.fileName)                
                   console.log(msg)
                  
                  wx.hideLoading()
                },
                fail(error) {
                  console.log(error);
                  console.log("上传失败")
                  wx.hideLoading()
                }
                
              })


          wx.setStorage({
            data: that.data.userobj,
            key: 'userinfo',
          })
        },
        filess(err) {
          console.log(err)
        },

      })
  },
  // // 上传图片或者视频

  // uploadsome: function (tempFilePaths) {
  //   var that = this
    
  //   console.log(tempFilePaths)
  //   wx.showLoading({
  //     title: '上传中',
  //   })

  //   wx.uploadFile({
  //     url: 'http://192.168.0.100:9377/qiniu/upload_file',
  //     filePath: tempFilePaths[0],
  //     name: 'file',
  //     formData: {
  //       'user': 'test',
  //     },
  //     success(res) {
  //       console.log(res.data);
  //       console.log(res)
  //       var msg = JSON.parse(res.data)
  //       var list = that.data.piclist
        
  //         //   console.log(that.data.piclist.picHeight)
        
  //       wx.hideLoading()
  //     },
  //     fail(error) {
  //       console.log(error);
  //       console.log("上传失败")
  //       wx.hideLoading()
  //     }
      
  //   })


  // },
    //更换头像接口
    portrait(fileName){
      var that = this
      var obj={fileName:fileName}
      mycenter.upd_head_portrait(obj).then(res=>{
        var my_details = that.data.my_details
        my_details.headPortrait = res.data.headPortrait
        that.setData({
          my_details:my_details
        })
      }).catch(error=>{
        console.log(error)
      })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      this.my_detail()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})